package com.lst.tools.basics.utils;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: Frank
 * @date: 2019/09/24 13:51
 * @version: V1.0
 */
public class DateUtils {

    public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_3 = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_4 = "yyyy/MM/dd";
    public static final String DATE_FORMAT_5 = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_FORMAT_6 = "yyyy/MM/dd HH:mm";
    public static final String DATE_FORMAT_7 = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_8 = "yyyyMMdd";
    public static final String DATE_FORMAT_9 = "yyyy.MM.dd";
    public static final String DATE_FORMAT_10 = "yyyy-MM";
    public static final String DATE_FORMAT_11 = "yyyy年MM月dd日";
    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    /**
     * 加分钟数
     *
     * @return
     */
    public static Date plusMinutes(Date date, Integer minutes) {
        if (minutes == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    /**
     * 减分钟数
     *
     * @return
     */
    public static Date minusMinutes(Date date, Integer minutes) {
        if (minutes == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.minusMinutes(minutes).toDate();
    }

    /**
     * 加天数
     *
     * @return
     */
    public static Date plusDays(Date date, Integer days) {
        if (days == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * 减天数
     *
     * @return
     */
    public static Date minusDays(Date date, Integer days) {
        if (days == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.minusDays(days).toDate();
    }

    /**
     * 加小时数
     *
     * @return
     */
    public static Date plusHours(Date date, Integer hours) {
        if (hours == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 减小时数
     *
     * @return
     */
    public static Date minusHours(Date date, Integer hours) {
        if (hours == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.minusHours(hours).toDate();
    }

    /**
     * 加减月份
     *
     * @return
     */
    public static Date plusMonths(Date date, Integer months) {
        if (months == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 加减year
     *
     * @return
     */
    public static Date plusYears(Date date, Integer years) {
        if (years == null) {
            return date;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static String format(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 解析日期
     *
     * @return
     */
    public static Date parse(String dateString, String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(dateString);
        } catch (Exception e) {
            LOGGER.error("parse date error:", e);
            return null;
        }
    }
}
